package com.huaqin.punan.updateui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	public static Handler mHandler;
	private TextView my_tv;
	private Button btnStart;
	private Button btnStop;
	protected static Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		context = this;
		
		my_tv = (TextView)findViewById(R.id.my_tv);
		btnStart = (Button)findViewById(R.id.my_btn);
		btnStop = (Button)findViewById(R.id.my_btn2);
		
		btnStart.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.e("punan", "StartService");
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, UpdateService.class);
				MainActivity.this.startService(intent);
			}});
		
		btnStop.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, UpdateService.class);
				MainActivity.this.stopService(intent);
			}});
		
		
		mHandler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch(msg.what){
				case 1:
					String message = (String)msg.obj;
					my_tv.setText(message);
					break;
				default:
					break;
				}

			}
		};
		
	}

	public Handler getHandler(){
		return mHandler;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
